import { ModalRef } from '@app/shared/utils'
import { Component, Output,OnInit, ViewChild, TemplateRef,EventEmitter } from '@angular/core'
import { FormBuilder, Validators, FormGroup } from '@angular/forms'
import { QlMessageService } from 'qloud-angular'
import { plainToClass } from 'class-transformer'
import { DictService } from '@app/shared/utils/dict.service'
import { ApiService } from '../../services/api.service'
import { ActivatedRoute } from '@angular/router'
import { ModalService } from '@app/shared/utils'
import { NetService, PageService } from '@app/core/http'
import { Router } from '@angular/router'
import { CursorService } from '@app/core/http/cursor.service'
import { Store } from '@ngxs/store'
import { ModalConfirmComponent } from '@app/shared/components/modal-confirm/modal-confirm.component'


@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.css'],
  providers: [ModalRef, ModalService, PageService]
})
export class SearchComponent implements OnInit {

  public baseitems = [{ value: '', label: '' }]

  // public channel = [{value:'线上挖掘',label:'线上挖掘'},{value:'线下挖掘',label:'线下挖掘'}]

  public formGroup: FormGroup
  // public chandelType = [
  //   { value: '1', label: '网银' },
  //   { value: '2', label: '手机app' }
  // ]
  // public tradArea = [
  //   { value: '1', label: '高新区' },
  //   { value: '2', label: '雁塔区' }
  // ]

  constructor(
    private modalRef: ModalRef,
    private fb: FormBuilder,
    private apiService: ApiService,
    public modal: ModalService,
    public dictService: DictService,
    public cursorService: CursorService,
    private message: QlMessageService,
    public pageService: PageService,
    private router: Router,
    private store: Store,
    public route: ActivatedRoute
  ) {
    this.formGroup = this.fb.group({
      "customerName":[null],
      "customerNo":[null],
      "contact":[null],
      "assetBal":[null],
      "professionalTitle":[null],
      "age":[null],
      "aum":[null],
      "wealthPotentialValue":[null],
      "marriaged":[null],
      "sex":[null],
      "customerStar":[null],
      "customerLevel":[null]
    })
  }

  ngOnInit() {}

  public onSubmit() {
    this.modalRef.close(true)
  }

  public saveQuery() {
    // let sql='select *'
    // if (this.formGroup.value.customerName !='' && this.formGroup.value.customerName != undefined) {
    //   sql = this.transform('customerName', this.formGroup.value.customerName, sql)
    // }
    // if(this.formGroup.value.customerNo !='' && this.formGroup.value.customerNo != undefined){
    //   sql = this.transform('customerNo', this.formGroup.value.customerNo, sql)
    // }
    // if(this.formGroup.value.contact !='' && this.formGroup.value.contact != undefined){
    //   sql = this.transform('contact', this.formGroup.value.contact, sql)
    // }
    // if(this.formGroup.value.assetBal !='' && this.formGroup.value.assetBal != undefined){
    //   sql = this.transform('assetBal', this.formGroup.value.assetBal, sql)
    // }
    // if(this.formGroup.value.professionalTitle !='' && this.formGroup.value.professionalTitle != undefined){
    //   sql = this.transform('professionalTitle', this.formGroup.value.professionalTitle, sql)
    // }
    // if(this.formGroup.value.age !='' && this.formGroup.value.age != undefined){
    //   sql = this.transform('age', this.formGroup.value.age, sql)
    // }
    // if(this.formGroup.value.aum !='' && this.formGroup.value.aum != undefined){
    //   sql = this.transform('aum', this.formGroup.value.aum, sql)
    // }
    // if(this.formGroup.value.wealthPotentialValue !='' && this.formGroup.value.wealthPotentialValue != undefined){
    //   sql = this.transform('wealthPotentialValue', this.formGroup.value.wealthPotentialValue, sql)
    // }
    // if(this.formGroup.value.marriaged !='' && this.formGroup.value.marriaged != undefined){
    //   sql = this.transform('marriaged', this.formGroup.value.marriaged, sql)
    // }
    // if(this.formGroup.value.sex !='' && this.formGroup.value.sex != undefined){
    //   sql = this.transform('sex', this.formGroup.value.sex, sql)
    // }
    // if(this.formGroup.value.customerStar !='' && this.formGroup.value.customerStar != undefined){
    //   sql = this.transform('customerStar', this.formGroup.value.customerStar, sql)
    // }
    // if(this.formGroup.value.customerLevel !='' && this.formGroup.value.customerLevel != undefined){
    //   sql = this.transform('customerLevel', this.formGroup.value.customerLevel, sql)
    // }
    // this.cursorService.statement = sql
    // this.apiService.saveQuery(this.cursorService).subscribe(data => {
    // })
    this.modalRef.close(true)
    this.router.navigate(['/customer/customer-list'],{queryParams:this.formGroup.value})
  }

 public transform(key: string, value: string, sql: string) {
    if (sql != 'select *') {
      sql += ' and ' + key + ' = ' + "'*" + value + "*'"
    } else {
      sql += ' where ' + key + ' = ' + "'*" + value + "*'"
    }
    return sql
  }
}
